﻿using Microsoft.AspNet.SignalR;
using System;
using System.Threading.Tasks;
using RssEasy.Core;
using LocalDAL;
using System.Collections.Generic;
using Microsoft.AspNet.SignalR.Hubs;

[HubName("ImHub")]
public class ImHub : Hub
{
    public static Dictionary<string, string> myiddict = new Dictionary<string, string>();
    public override Task OnDisconnected(bool stopCalled)
    {
        UserList entity = new UserList();
        entity.columnvalue("connectionid", null);
        entity.timestamp("offlinetime");
        entity.update().where("connectionid=@connectionid", Context.ConnectionId).get_first_rows();

        if (entity.select("myid").where("connectionid=@connectionid", Context.ConnectionId).get_first_rows())
        {
            myiddict.Remove(entity["myid"]);
        }
        return base.OnDisconnected(stopCalled);
    }
    public string GetConnID(string myid)
    {
        if (myiddict.ContainsKey(myid))
        {
            return myiddict[myid];
        }
        UserList entity = new UserList();
        if (entity.select("connectionid").where("myid=@myid and connectionid is not null", myid).get_first_rows())
        {
            myiddict.Add(myid, entity["connectionid"]);
            return entity["connectionid"];
        }
        this.AdapterCaller("onUserOffline", myid);
        return null;
    }
    public void onError(Exception ex)
    {
        this.AdapterCaller("onError", ex.Message);
    }
    public void AdapterCaller(string adapter, string data)
    {
        Clients.Caller.adapter(adapter, data);
    }
    public void AdapterClient(string adapter, string connid, string data)
    {
        Clients.Client(connid).adapter(adapter, data);
    }
    public void AdapterClient(string adapter, string connid, string data, string marker)
    {
        Clients.Client(connid).adapter(adapter, data, marker);
    }
    public void AdapterClient(string adapter, string connid, string myid, string data, string marker)
    {
        Clients.Client(connid).adapter(adapter, myid, data, marker);
    }
    public void AdapterClient(string adapter, string connid, string myid, string data, int marker)
    {
        Clients.Client(connid).adapter(adapter, myid, data, marker);
    }
    public void AdapterAll(string adapter, string data, string marker)
    {
        Clients.All.adapter(adapter, data, marker);
    }
    /// <summary>
    /// 用户登陆
    /// </summary>
    /// <param name="myid"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public int UserLogin(string myid)
    {
        myiddict.Remove(myid);
        myiddict.Add(myid, Context.ConnectionId);

        UserList entity = new UserList();
        entity.remove("myid");
        entity.keyvalue("connectionid", Context.ConnectionId).timestamp("unlinetime");
        entity.columnvalue("offlinetime", "NULL");
        entity.update().where("myid=@myid", myid).submit();
        return myiddict.Count;
    }
    public string GetChatList(string myid, string tomyid)
    {
        ChatList list = new ChatList();
        list.select("myid,msg,shijian").where("myid=@myid and tomyid=@tomyid", tomyid, myid).query();
        list.delete().where("myid=@myid and tomyid=@tomyid", tomyid, myid).submit();
        return JsonExtend.toKeyValue(list.datatable);
    }
    public void BroadCast(string data, string marker)
    {
        this.AdapterAll("onReceiveBroadCast", data, marker);
    }
    public void Notify(string myid, string data, string marker)
    {
        try
        {
            string connid = GetConnID(myid);
            if (connid != null)
            {
                this.AdapterClient("onReceiveNotify", connid, data, marker);
            }
        }
        catch (Exception ex)
        {
            this.onError(ex);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="myid"></param>
    /// <param name="tomyid"></param>
    /// <param name="msg"></param>
    /// <param name="save">0、离线保存,1、都保存,2都不保存</param>
    /// <returns></returns>
    public void Chat(string myid, string tomyid, string data, int save)
    {
        try
        {
            string connid = GetConnID(tomyid);
            if (connid != null)
            {
                this.AdapterClient("onReceiveChat", connid, myid, data, DateTimeExtend.timestamp());
                if (save == 0) return;
            }
            if (save != 2)
            {
                ChatList chat = new ChatList();
                chat.keyvalue("myid", myid).keyvalue("tomyid", tomyid).keyvalue("msg", data).timestamp();
                chat.append().submit();
            }
        }
        catch (Exception ex)
        {
            this.onError(ex);
        }
    }
    public void Chat(string myid, string tomyid, string data)
    {
        this.Chat(myid, tomyid, data, 0);
    }
}